package com.my.learn.thread.executor;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.my.learn.thread.MyCallable;
import com.my.learn.thread.TestThread;

import java.util.concurrent.*;

public class ScheduledExecutorTest {

    private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("test-pool-%d").build();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10, new NamedThreadFactory("ScheduledThread"));

        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(3,  namedThreadFactory);
        //周期性任务
        scheduledExecutorService.scheduleAtFixedRate(new TestThread(1), 1, 1, TimeUnit.SECONDS);
        //延时任务 带返回值
        ScheduledFuture<Integer> scheduledFuture = scheduledExecutorService.schedule(new MyCallable(1), 3, TimeUnit.SECONDS);
        scheduledExecutorService.schedule(new TestThread(2), 1, TimeUnit.SECONDS);
        scheduledExecutorService.schedule(new TestThread(3), 1, TimeUnit.SECONDS);
        System.out.println(scheduledFuture.get());
    }
}
